package com.itheima.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.entity.Result;
import com.itheima.pojo.MemberReportVo;
import com.itheima.service.MemberService;
import com.itheima.service.ReportService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@RequestMapping("/report")
public class ReportController {
    @Reference
    ReportService reportService;
    @Reference
    MemberService memberService;

    @RequestMapping("/getMemberReport")
    public Result getMemberReport(@RequestBody MemberReportVo vo) {
        List<String> months = new ArrayList<>();
        Date start = vo.getStart();
        Date end = vo.getEnd();
        //默认取12个月
        long month = 12;
        //得到过去1年的月份
        DateTime dateTime = DateUtil.offsetMonth(new Date(), -12);
        if (null != start && null != end) {
            //页面传了就取页面的
            month = DateUtil.betweenMonth(start, end, false);
            dateTime = DateTime.of(start);
        }
        for (int i = 0; i < month; i++) {
            months.add(DateUtil.offsetMonth(dateTime, i).toString("yyyy-MM"));
        }

        List<Integer> memberCount = reportService.getMemberReport(months);
        Map map = new HashMap<>();
        map.put("months", months);
        map.put("memberCount", memberCount);
        return Result.success("", map);
    }

    @RequestMapping("/getSetmealReport")
    public Result getSetmealReport() {
        List<Map> maps = reportService.getSetmealReport();
        return Result.success("", maps);
    }


    public static void main(String[] args) {
        DateTime dateTime = DateUtil.offsetMonth(new Date(), -12);
//        System.out.println(dateTime.toString("yyyy-MM"));
        List<String> months = new ArrayList<>();
        for (int i = 0; i < 12; i++) {
            months.add(DateUtil.offsetMonth(dateTime, i).toString("yyyy-MM"));
        }

        String dateStr1 = "2017-03-01 22:33:23";
        Date date1 = DateUtil.parse(dateStr1);

        String dateStr2 = "2017-04-01 23:33:23";
        Date date2 = DateUtil.parse(dateStr2);

//相差一个月，31天
        long betweenDay = DateUtil.betweenMonth(date1, date2, false);
        dateTime = DateTime.of(date2);
        System.out.println(dateTime);
    }

    @RequestMapping("/getGenderReport")
    public Result getGenderReport() {
        List<Map> list = memberService.getGenderReport();
        return Result.success("", list);
    }

    @RequestMapping("/getMemberAgeReport")
    public Result getMemberAgeReport() {
        try {
            List<Map<String, Object>> list = memberService.findMemberAgeCount();
            Map<String, Object> map = new HashMap<>();
            map.put("ageCount", list);
            List<String> memberAge = new ArrayList<>();
            for (Map<String, Object> maplist : list) {
                String name = (String) maplist.get("name");
                memberAge.add(name);
            }
            map.put("memberAge", memberAge);
            return Result.success("", map);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("");
        }
    }
}
